﻿Imports System.Text.RegularExpressions

Module SeriesFileParser

    Dim _log As NLog.Logger = NLog.LogManager.GetLogger("RenamePlugin")

    Private Function StripJunk(ByVal _filename As String) As String

        Dim _rt As String = _filename.ToLower

        _rt = _rt.Replace("hdtv", "")
        _rt = _rt.Replace("720p", "")
        _rt = _rt.Replace("1080p", "")
        _rt = _rt.Replace("hdtv", "")
        _rt = _rt.Replace("dvdrip", "")
        _rt = _rt.Replace("x264", "")


        Return _rt

    End Function

    Friend Function ParseSeriesFile(ByVal _file As String, ByVal _folder As String) As SeriesParseResult

        Dim _rt As New SeriesParseResult
        Dim _tmp_file As String
        Dim _tmp_folder As String
        Dim _regex As Regex = New Regex("^((?<series_name>.+?)[. _-]+)?s(?<season_num>\d+)[. _-]*e(?<ep_num>\d+)(([. _-]*e|-)(?<extra_ep_num>(?!(1080|720)[pi])\d+))*[. _-]*((?<extra_info>.+?)((?<![. _-])-(?<release_group>[^-]+))?)?$")
        Dim _regex_folder As Regex = New Regex("^((?<series_name>.+?)[. _-]+)?s(?<season_num>\d+)[. _-]")

        _tmp_file = _file.ToLower
        _tmp_folder = _folder.ToLower

        '_tmp = StripJunk(_tmp)

        Dim mc As Match = _regex.Match(_tmp_file)

        If mc.Success = True Then

            _log.Debug("Serienname: {0}", mc.Groups("series_name").Value)
            _log.Debug("Episode Nummer: {0}", mc.Groups("ep_num").Value)
            _log.Debug("Staffel Nummer: {0}", mc.Groups("season_num").Value)
            _log.Debug("Extra Episoden Nummer: {0}", mc.Groups("extra_ep_num").Value)
            _log.Debug("Extra Info: {0}", mc.Groups("extra_info").Value)
            _log.Debug("Rel Group: {0}", mc.Groups("release_group").Value)

            _tmp_file = mc.Groups("series_name").Value

            _tmp_file = _tmp_file.Replace(".", Space(1))

            If _tmp_file.Contains("-") Then ' Should remove Group Prefix Like sof-theflash.mkv
                _tmp_file = _tmp_file.Remove(0, _tmp_file.IndexOf("-"))
            End If

            _tmp_file = _tmp_file.Replace(".", Space(1))

            If IsNumeric(mc.Groups("ep_num").Value) = False Or IsNumeric(mc.Groups("ep_num").Value) = False Then
                'No Valid Parsing
                _log.Error("Es konnte keinen Episode oder Staffel aus dem Dateinamen extrahiert werden")
            Else
                _rt.EpisodeNumber = Integer.Parse(mc.Groups("ep_num").Value)
                _rt.SeasonNumber = Integer.Parse(mc.Groups("season_num").Value)

                _log.Info("Extrahierten AutoQuery: {0}", _rt.QueryString)
                _log.Info("Extrahierten Episoden Nummer: {0}", _rt.EpisodeNumber)
                _log.Info("Extrahierten Staffel Nummer: {0}", _rt.SeasonNumber)

            End If

        End If

        mc = _regex_folder.Match(_tmp_folder)

        If mc.Success Then

            _tmp_folder = mc.Groups("series_name").Value
            _tmp_folder = _tmp_folder.Replace(".", Space(1))
            _tmp_folder = _tmp_folder.Replace("_", Space(1))

        End If

        _rt.QueryString = String.Format("{0},{1}", _tmp_folder, _tmp_file)


        Return _rt

    End Function


End Module

Public Class SeriesParseResult
    Public QueryString As String
    Public EpisodeNumber As Integer
    Public SeasonNumber As Integer
End Class
